/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VariableFrame.java
 *
 * Created on 2010-5-31, 11:50:04
 */

package org.free.jshoc;

import java.util.HashMap;
import java.util.Iterator;
import javax.swing.table.AbstractTableModel;
import org.free.jshoc.compute.Notifiable;

/**
 *
 * @author juntao
 */
public class VariableFrame extends javax.swing.JFrame {
    private HashMap<String, Object> table;
    private VariableTableModel model;
    private Notifiable entity;
    
    /** Creates new form VariableFrame */
    public VariableFrame(Notifiable entity, HashMap<String, Object> table) {
        this.entity = entity;
        this.table = table;
        this.model = new VariableTableModel(this.table);
        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelVariable = new javax.swing.JPanel();
        spVariable = new javax.swing.JScrollPane();
        tableVariable = new javax.swing.JTable();
        labName = new javax.swing.JLabel();
        textName = new javax.swing.JTextField();
        labValue = new javax.swing.JLabel();
        textValue = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnSubmit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelVariable.setName("panelVariable"); // NOI18N

        spVariable.setName("spVariable"); // NOI18N

        tableVariable.setModel(this.model);
        tableVariable.setColumnSelectionAllowed(true);
        tableVariable.setName("tableVariable"); // NOI18N
        spVariable.setViewportView(tableVariable);
        tableVariable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        labName.setFont(new java.awt.Font("宋体", 1, 12));
        labName.setText("Name:");
        labName.setName("labName"); // NOI18N

        textName.setName("textName"); // NOI18N

        labValue.setFont(new java.awt.Font("宋体", 1, 12));
        labValue.setText("Value:");
        labValue.setName("labValue"); // NOI18N

        textValue.setName("textValue"); // NOI18N

        btnAdd.setText("Add");
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSubmit.setText("Submit");
        btnSubmit.setName("btnSubmit"); // NOI18N
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelVariableLayout = new javax.swing.GroupLayout(panelVariable);
        panelVariable.setLayout(panelVariableLayout);
        panelVariableLayout.setHorizontalGroup(
            panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(spVariable, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
            .addGroup(panelVariableLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panelVariableLayout.createSequentialGroup()
                        .addComponent(labName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labValue)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textValue, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnSubmit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .addContainerGap())
        );

        panelVariableLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnSubmit});

        panelVariableLayout.setVerticalGroup(
            panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVariableLayout.createSequentialGroup()
                .addComponent(spVariable, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labName)
                    .addComponent(labValue)
                    .addComponent(textValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd))
                .addGap(18, 18, 18)
                .addGroup(panelVariableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSubmit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelVariableLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCancel, btnSubmit});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelVariable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelVariable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        String name = textName.getText();
        String value = textValue.getText();
        //
        this.model.addRow(new Object[]{name, value});
        textName.setText("");
        textValue.setText("");
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        // TODO add your handling code here:
        
        Object[][] data = model.getDataArray();
        for(int i = 0; i < data.length; i++){
            String name = (String)data[i][0];
            Object value = data[i][1];
            String expr = "var "+name+" = "+value+";";
            entity.callback(expr);
        }
        
        this.dispose();
    }//GEN-LAST:event_btnSubmitActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel labName;
    private javax.swing.JLabel labValue;
    private javax.swing.JPanel panelVariable;
    private javax.swing.JScrollPane spVariable;
    private javax.swing.JTable tableVariable;
    private javax.swing.JTextField textName;
    private javax.swing.JTextField textValue;
    // End of variables declaration//GEN-END:variables



    class VariableTableModel extends AbstractTableModel{
        private HashMap<String, Object> table;
        private Object[][] model;

        public VariableTableModel(HashMap<String, Object> table){
            this.table = table;
            build();
        }

        private void build(){
            model = new Object[table.size()][2];
            Iterator it = table.keySet().iterator();
            int i = 0;
            while(it.hasNext()){
                String key = (String)it.next();
                Object value = table.get(key);
                model[i][0] = key;
                model[i][1] = value;
                i++;
            }
        }

        public int getRowCount() {
            return model.length;
        }

        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            return model[rowIndex][columnIndex];
        }

        public void addRow(Object[] cells){
           // System.arraycopy(model, 0, cells, WIDTH, WIDTH);
            Object[][] dst = new Object[model.length+1][2];
            System.arraycopy(model, 0, dst, 0, model.length);
            dst[model.length] = cells;
            model = dst;
            fireTableDataChanged();
            System.err.println(model);
        }

        public Object[][] getDataArray(){
            return model;
        }
    }
}
